//----------------------------------
//--------Shovel SWEP---------------
//-----------Made by Brian Nevec----
//----------------------------------
//---Part of the SMOD Weapons Pack--
//----------------------------------

//if on server
if SERVER then

	//send file
	AddCSLuaFile("shared.lua");

	//hold type
	SWEP.HoldType			= "melee";

end

//else if on client
if CLIENT then

	//info
	SWEP.PrintName			= "Shovel";
	SWEP.Author				= "Brian Nevec";
	SWEP.Slot				= 0;
	SWEP.SlotPos			= 3;
	SWEP.Purpose			= "Used for beating up people";
	SWEP.Instructions		= "Left Click: Hit, Right Click: Stab";

	//category
	SWEP.Category			= "SMOD";

end

//is it spawnable?
SWEP.Spawnable				= true;
SWEP.AdminSpawnable			= false;

//models
SWEP.ViewModel				= "models/weapons/v_shovel.mdl";
SWEP.WorldModel				= "models/weapons/w_shovel.mdl";

//more info
SWEP.Weight					= 25;
SWEP.AutoSwitchTo			= false;
SWEP.AutoSwitchFrom			= false;

//primary
SWEP.Primary =
{
	ClipSize				= -1,
	DefaultClip				= -1,
	Automatic				= true,
	Ammo					= "none"
}

//secondary
SWEP.Secondary =
{
	ClipSize				= -1,
	DefaultClip				= -1,
	Automatic				= true,
	Ammo					= "none"
}

//sounds
SWEP.Sounds =
{
	Swing = Sound("weapons/shovel/shovel_fire.wav"),
	Hit1 = Sound("weapons/shovel/shovel_hit1.wav"),
	Hit2 = Sound("weapons/shovel/shovel_hit2.wav"),
	Hit3 = Sound("weapons/shovel/shovel_hit3.wav"),
	Stab = Sound("weapons/shovel/shovel_stab.wav"),
}

SWEP.NextAnim = 1;

//on init
function SWEP:Initialize()

	//if on server
	if SERVER then

		//set hold type
		self:SetWeaponHoldType(self.HoldType);

	end

end

//primary attack
function SWEP:PrimaryAttack()

	if SERVER then

		self.NextAnim = self.NextAnim + 1;

		if self.NextAnim == 3 then

			self.NextAnim = 1;

		end

		self.Weapon:SendWeaponAnim(_G["ACT_VM_PRIMARYATTACK_"..self.NextAnim]);

		timer.Simple(0.5,
		function()

			if ValidEntity(self.Owner) && ValidEntity(self.Weapon) then

				local Origin = self.Owner:GetShootPos();
				local Target = self.Owner:GetAimVector() * 110;
				local Max = Vector(16,16,16);
				local Min = Max * -1;

				local Trace,Ent = self:HullAttack(Origin,Target,Min,Max,50,128,5);

				if ValidEntity(Trace.Entity) || ValidEntity(Ent) then

					self.Owner:EmitSound(self.Sounds["Hit"..math.random(1,3)]);

					local Ang = Angle(math.Rand(-5,-7),math.Rand(5,7),0)

					if self.NextAnim == 1 then

						Ang = Angle(math.Rand(-5,-7),math.Rand(-5,-7),0);

					end

					self.Owner:ViewPunch(Ang);

					if Trace.Entity:IsNPC() || Trace.Entity:IsPlayer() then

						for I = 0,2 do

							local FX = EffectData();
							FX:SetOrigin(Trace.HitPos + (Trace.HitNormal * I) / 2);
							util.Effect("BloodImpact",FX,true,true);

						end

					end

				else

					self.Owner:EmitSound(self.Sounds.Swing);

				end

			end

		end)

	end

	//primary delay
	self.Weapon:SetNextPrimaryFire(CurTime() + 0.80);

	//return false
	return false;

end

//secondary
function SWEP:SecondaryAttack()

	if SERVER then

		self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK_3);

		timer.Simple(0.3,
		function()

			self.Owner:EmitSound(self.Sounds.Swing);

			local Origin = self.Owner:GetShootPos();
			local Target = self.Owner:GetAimVector() * 140;
			local Max = Vector(16,16,16);
			local Min = Max * -1;

			local Trace,Ent = self:HullAttack(Origin,Target,Min,Max,80,128,5);

			if ValidEntity(Trace.Entity) || ValidEntity(Ent) then

				if Trace.Entity:IsNPC() || Trace.Entity:IsPlayer() then

					for I = 0,2 do

						local FX = EffectData();
						FX:SetOrigin(Trace.HitPos + (Trace.HitNormal * I) / 2);
						util.Effect("BloodImpact",FX,true,true);

					end

				end

			end

			timer.Simple(0.1,self.Owner.EmitSound,self.Owner,self.Sounds.Stab);

		end)

	end

	//secondary delay
	self.Weapon:SetNextSecondaryFire(CurTime() + 1);

	//return
	return false;

end

//when we deploy
function SWEP:Deploy()

	//run the deploy animation
	self.Weapon:SendWeaponAnim(ACT_VM_DRAW);

	//and return
	return true;

end

function SWEP:HullAttack(Origin,Dest,Min,Max,Damage,Type,Force)

	local Ent = self.Owner:TraceHullAttack(Origin,Dest,Min,Max,Damage,Type,Force,true);

	local T = {};
	T.start = Origin;
	T.endpos = T.start + Dest;
	T.filter = {self.Owner};

	return util.TraceLine(T),Ent;

end